// pages/mine/CompanyHomepage/CompanyHomepage.js
import WeCropper from '../../../we-cropper/we-cropper.js';

const app = getApp();
const device = wx.getSystemInfoSync();
const width = device.windowWidth;
const height = device.windowHeight;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    company: {},
    company_platfrom: [],
    company_anchor: [],
    company_photo: [],
    listdata:[
      {
        image:''
      }
    ],
    img_logo: 'https://www.zhibohome.net/images/CompanyHome/gsjs_mrtb.png',
    // 按钮显示
    tailor_show: true,
    // 裁剪图片
    cropperOpt: {
      id: 'cropper',
      width,
      height,
      scale: 2.5,
      zoom: 8,
      cut: {
        x: (width - 300) / 2,
        y: (height - 300) / 2,
        width: 622 /2,
        height: 352 /2,
      }
    },
  },
  // 编辑公司
  CompanyBasis(){
    
    wx.navigateTo({
      url: '../CompanyBasis/CompanyBasis',
    })
  },
  // 公司预览
  CompanyPreview(){
    wx.navigateTo({
      url: '../CompanyPreview/CompanyPreview',
    })
  },
  // 入驻平台
  CompanyPlatform(){
    wx.navigateTo({
      url: '../CompanyPlatform/CompanyPlatform',
    })
  },
  // 明星主播
  CompanyStar(){
    wx.navigateTo({
      url: '../CompanyStar/CompanyStar',
    })
  },
  // 公司相册
  CompanyPhoto(){
    let that = this;
    that.setData({
      tailor_show: false,
    })
  },
  // 入驻平台删除
  btn_delete(e){
    var del_id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var cpy_platfrom = this.data.company_platfrom;
    cpy_platfrom.splice(index,1);
    this.setData({
      company_platfrom:cpy_platfrom
    })
    var that = this;
    wx.request({
      method: 'POST',
      url: app.globalData.url + 'api/company/plat/anchor/photo/del',
      data: {
        token: app.globalData.token,
        type:'1',
        del_id:del_id
      },
      success: function (e) {
        wx.showToast({
          title: '删除成功',
          icon: 'none',
        })
      }
    })
  },
  // 明星主播删除
  btn_delete_star(e){
    var del_id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var cpy_anchor = this.data.company_anchor;
    cpy_anchor.splice(index, 1);
    this.setData({
      company_anchor: cpy_anchor
    })
    var that = this;
    wx.request({
      method: 'POST',
      url: app.globalData.url + 'api/company/plat/anchor/photo/del',
      data: {
        token: app.globalData.token,
        type: '2',
        del_id: del_id
      },
      success: function (e) {
        wx.showToast({
          title: '删除成功',
          icon: 'none',
        })
      }
    })
  },
  // 公司相册删除
  btn_del_photo(e){
    var del_id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var cpy_photo = this.data.company_photo;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否删除照片',
      success(res) {
        if (res.confirm) {
          cpy_photo.splice(index, 1);
          that.setData({
            company_photo: cpy_photo
          })
          
          wx.request({
            method: 'POST',
            url: app.globalData.url + 'api/company/plat/anchor/photo/del',
            data: {
              token: app.globalData.token,
              type: '3',
              del_id: del_id
            },
            success: function (e) {
              wx.showToast({
                title: '删除成功',
                icon: 'none',
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 去下载APP
  CompanyBtn() {
    wx.showToast({
      title: '请下载直播之家App~',
      icon: 'none',
    })
  },
  //选择 裁剪图片
  click_close: function (e) {
    this.setData({
      tailor_show: true,
    });
  },
  touchStart(e) {
    this.wecropper.touchStart(e)
  },
  touchMove(e) {
    this.wecropper.touchMove(e)
  },
  touchEnd(e) {
    this.wecropper.touchEnd(e)
  },
  getCropperImage(e) {
    var index = this.data.index;
    wx.showLoading({
      title: '加载中~',
      icon: "none",
    })
    let that = this;
    this.wecropper.getCropperImage((src) => {
      if (src) {
        var image_base64 = wx.getFileSystemManager().readFileSync(src, "base64");
        var abc = that.data.listdata;
        var list = that.data.company_photo; 
        var obj = {};
        obj.img_url = src;
        abc[0].image = image_base64;
        list.push(obj)
        that.setData({
          tailor_show: true,
          company_photo: list
        });
        var datalist = JSON.stringify(abc);
        wx.request({
          method: 'POST',
          url: app.globalData.url + 'api/company/photo/album',
          data: {
            token: app.globalData.token,
            image: datalist
          },
          success: function (data) {
            that.init();
          }
        });
        wx.hideLoading();

      } else {
        console.log('获取图片地址失败，请稍后重试')
      }

    });
  },
  uploadTap() {
    const self = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0]
        //  获取裁剪图片资源后，给data添加src属性及其值
        self.wecropper.pushOrign(src)
      }
    })
  },
  //裁剪图片 初始化
  init: function () {
    const {
      cropperOpt
    } = this.data

    new WeCropper(cropperOpt)
      .on('ready', (ctx) => {
        console.log(`wecropper is ready for work!`)
      })
      .on('beforeImageLoad', (ctx) => {
        console.log(`before picture loaded, i can do something`)
        console.log(`current canvas context:`, ctx)
        wx.showToast({
          title: '上传中',
          icon: 'loading',
          duration: 20000
        })
      })
      .on('imageLoad', (ctx) => {
        console.log(`picture loaded`)
        console.log(`current canvas context:`, ctx)
        wx.hideToast()
      })
      .on('beforeDraw', (ctx, instance) => {
        console.log(`before canvas draw,i can do something`)
        console.log(`current canvas context:`, ctx)
      })
      .updateCanvas()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init();
    var that = this;
    wx.request({
      method: 'POST',
      url: app.globalData.url + 'api/company/index',
      data: {
        token: app.globalData.token
      },
      success: function (e) {
        console.log(e)
        that.setData({
          company: e.data.data.company,
          company_platfrom: e.data.data.company_platfrom,
          company_anchor: e.data.data.company_anchor,
          company_photo: e.data.data.company_photo
        })
      }
    })
  },
  handleChange: function (e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.request({
      method: 'POST',
      url: app.globalData.url + 'api/company/index',
      data: {
        token: app.globalData.token
      },
      success: function (e) {
        that.setData({
          company: e.data.data.company,
          company_platfrom: e.data.data.company_platfrom,
          company_anchor: e.data.data.company_anchor,
          company_photo: e.data.data.company_photo
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})